package com.das.mechanic_base.widget;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Point;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import com.das.mechanic_base.R;
import com.das.mechanic_base.utils.X3StringUtils;
import com.das.mechanic_base.utils.X3ToastUtils;

/**
 * Created by chensi on 2018/4/24.
 */

public class X3GradeInputDialog extends Dialog implements View.OnClickListener, TextWatcher {

    private Point mScreenPoint = new Point();
    private Activity mRootActivity = null;

    private TextView tv_cancel;
    private TextView tv_affirm;
    private EditText et_input;

    private String discountStr;

    /**
     * 一个按钮Dialog
     *
     * @param arg
     */
    public X3GradeInputDialog(Activity arg) {
        super(arg, R.style.inputDialog);
        mRootActivity = arg;
    }

    /**
     * 通过控件的Id获取对于的控件，如果没有则加入views
     *
     * @param viewId
     * @return
     */
    public <T extends View> T getView(int viewId) {
        View view = null;
        view = findViewById(viewId);
        return (T) view;
    }

    @SuppressWarnings("deprecation")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setBackgroundDrawable(new BitmapDrawable());
        this.setContentView(R.layout.x3_grade_input_dialog);
        initDialogWindow();
        initView();
    }

    void initView() {
        tv_cancel = findViewById(R.id.tv_cancel);
        tv_affirm = findViewById(R.id.tv_affirm);
        et_input = findViewById(R.id.et_input);

        tv_cancel.setOnClickListener(this);
        tv_affirm.setOnClickListener(this);
        et_input.addTextChangedListener(this);
    }

    @Override
    public void show() {
        super.show();
        handler.postDelayed(runnable,200);
    }

    private Handler handler = new Handler();

    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            et_input.setFocusable(true);
            et_input.setFocusableInTouchMode(true);
            et_input.requestFocus();
            InputMethodManager inputManager =
                    (InputMethodManager) et_input.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
            inputManager.showSoftInput(et_input, 0);
        }
    };

    @SuppressLint("NewApi")
    @SuppressWarnings("deprecation")
    private void initDialogWindow() {
        Window dialogWindow = getWindow();
        dialogWindow.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);

        WindowManager m = mRootActivity.getWindowManager();
        Display d = m.getDefaultDisplay(); // 获取屏幕宽、高用

        try {
            d.getSize(mScreenPoint);
        } catch (NoSuchMethodError ignore) {
            mScreenPoint.x = d.getWidth();
            mScreenPoint.y = d.getHeight();
        }

        WindowManager.LayoutParams p = dialogWindow.getAttributes(); // 获取对话框当前的参数值
        p.width = (int) (mScreenPoint.x * 0.70);    // 宽度设置为屏幕的0.9
        dialogWindow.setAttributes(p);
    }

    IOnClickGrade iOnClickGrade;

    public void setiOnClickGrade(IOnClickGrade iOnClickGrade) {
        this.iOnClickGrade = iOnClickGrade;
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        discountStr = s.toString();
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        String trim = s.toString().trim();
        if (!X3StringUtils.isEmpty(trim)) {
            Double currentInput = 0.0;
            if (trim.contains(".")) {
                String[] split = trim.split("\\.");
                if (split.length <= 1) {
                    return;
                }
                String s1 = split[1];
                if (X3StringUtils.isEmpty(s1)) {
                    return;
                }
                if (s1.length() == 2) {
                    et_input.setText(discountStr);
                    try {
                        String trim1 = et_input.getText().toString().trim();
                        et_input.setSelection(trim1.length());
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    @Override
    public void afterTextChanged(Editable s) {

    }

    public interface IOnClickGrade{
        void iOnClickUploadGrade(String grade);
    }

    @Override
    public void dismiss() {
        super.dismiss();
        if (handler != null) {
            handler.removeCallbacks(runnable);
        }
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.tv_cancel) {
            this.dismiss();
        } else if (id == R.id.tv_affirm) {
            String content = et_input.getText().toString();
            if (X3StringUtils.isEmpty(content)) {
                X3ToastUtils.showMessage(mRootActivity.getString(R.string.x3_input_grade_notice));
                return;
            }
            double i = Double.parseDouble(content);
//                int i = Integer.parseInt(content);
            if (i > 100 || i < 0) {
                X3ToastUtils.showMessage(mRootActivity.getString(R.string.x3_input_grade_notice));
                return;
            }
            this.dismiss();
            if (iOnClickGrade != null) {
                iOnClickGrade.iOnClickUploadGrade(content);
            }
        }
    }
}


